package mobi.hami.huapin.service.impl;

import com.github.pagehelper.PageHelper;
import mobi.hami.huapin.dao.NoteInfoMapper;
import mobi.hami.huapin.dao.NoteScheduleMapper;
import mobi.hami.huapin.dto.NoteInfoForUserDTO;
import mobi.hami.huapin.dto.NoteSortForUserDTO;
import mobi.hami.huapin.dto.UserNoteSchedule;
import mobi.hami.huapin.model.NoteSchedule;
import mobi.hami.huapin.service.NoteScheduleService;
import mobi.hami.huapin.service.NoteSortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Create by 小峰 on 2018/9/13.
 * 纸条-排期
 */
@Service
public class NoteScheduleServiceImpl implements NoteScheduleService {

    @Autowired
    private NoteScheduleMapper noteScheduleMapper;
    @Autowired
    NoteInfoMapper noteInfoMapper;
    @Autowired
    NoteScheduleMapper scheduleMapper;
    @Autowired
    NoteSortService sortService;


    /**
     * 随机获取3条纸条信息
     * @return
     */
    public List<NoteSchedule> FindRandLimitThree() {
        return noteScheduleMapper.FindRandLimitThree();
    }

    /**
     * 获取通用纸条列表(spaceid 目前未用)
     *
     * @return
     */
    @Override
    public List<NoteSortForUserDTO> getNoteScheduleBySpaceID(int spaceid) {

        List<NoteInfoForUserDTO> commonnotes = scheduleMapper.queryCommonNotes();

        List<NoteSortForUserDTO> noteSortForUserDTOS = sortService.querySortForUser();
        noteSortForUserDTOS.forEach(a->{
            a.setNotes(commonnotes.stream().filter(note->note.getSort_id() == a.getId()).collect(Collectors.toList()));
        });

        return noteSortForUserDTOS;
    }

    /**
     * 获取用户提交的，且审核通过的纸条（目前只显示最新一条）
     *
     * @param userid
     * @return
     */
    @Override
    public List<NoteInfoForUserDTO> getUserNote(int userid) {

        PageHelper.startPage(1,1);
        List<NoteInfoForUserDTO> userNotes = noteInfoMapper.queryUserNotes(userid);

        return userNotes;
    }

    /**
     * 返回通用纸条+用户自己添加的
     *
     * @param reqmap
     * @return
     */
    @Override
    public UserNoteSchedule getUserNoteWithCommon(Map<String, String> reqmap) {

        UserNoteSchedule userNoteSchedule = new UserNoteSchedule();
        userNoteSchedule.setCommonNoteWithSort(getNoteScheduleBySpaceID(Integer.parseInt(reqmap.get("spaceid"))));

        List<NoteInfoForUserDTO> userNotes =getUserNote(Integer.parseInt(reqmap.get("userid")));
        if (userNotes != null)
        {
            userNoteSchedule.setInfoForUserDTOS(userNotes);
        }

        return userNoteSchedule;
    }
}
